package com.xiaobai.zhuangxiuguanjia.activities;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.xiaobai.zhuangxiuguanjia.R;
import com.xiaobai.zhuangxiuguanjia.adapters.MyPagerAdapter;
import com.xiaobai.zhuangxiuguanjia.fragments.FindCompanyFragment;
import com.xiaobai.zhuangxiuguanjia.fragments.HeadFragment;
import com.xiaobai.zhuangxiuguanjia.fragments.MineFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity
        implements ViewPager.OnPageChangeListener, FindCompanyFragment.OnFragmentInteractionListener, RadioGroup.OnCheckedChangeListener {

    private RadioButton homeTab;
    private RadioButton picTab;
    private RadioButton companyTab;
    private RadioButton mineTab;
    private ViewPager mainPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        mainPager = (ViewPager) findViewById(R.id.main_pager);
        RadioGroup tabGroup = (RadioGroup) findViewById(R.id.main_tab_group);
        homeTab = (RadioButton) findViewById(R.id.main_tab_home);
        picTab = (RadioButton) findViewById(R.id.main_tab_pic);
        companyTab = (RadioButton) findViewById(R.id.main_tab_company);
        mineTab = (RadioButton) findViewById(R.id.main_tab_mine);

        homeTab.setChecked(true);

        // 测试
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HeadFragment());
        fragments.add(new MineFragment());
        fragments.add(new FindCompanyFragment());
        fragments.add(new MineFragment());

        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
        mainPager.setAdapter(adapter);
        mainPager.setOnPageChangeListener(this);
        tabGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                homeTab.setChecked(true);
                break;
            case 1:
                picTab.setChecked(true);
                break;
            case 2:
                companyTab.setChecked(true);
                break;
            case 3:
                mineTab.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_tab_home:
                mainPager.setCurrentItem(0);
                break;
            case R.id.main_tab_pic:
                mainPager.setCurrentItem(1);
                break;
            case R.id.main_tab_company:
                mainPager.setCurrentItem(2);
                break;
            case R.id.main_tab_mine:
                mainPager.setCurrentItem(3);
                break;
        }
    }
}
